package com.example.module_word.viewmodel

import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.module_word.data.DanCiData
import com.example.module_word.room.DanCiDataBase
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.internal.toLongOrDefault
import java.util.Date
import java.util.Locale

class HomeViewModel : ViewModel() {

    private val date by lazy { Date() }

    /**
     * 获取日期 天
     */
    fun getDay(): String {
        return String.format(Locale.getDefault(), "%td", date)
    }

    /**
     * 获取日期 年和月
     */
    fun getYearAndMonth(): String {
        return String.format("%tY", date) + "/" + String.format("%tm", date)
    }

    //签到状态键
    private val signInStateKey = "SIGNINSTATEKEY"

    //签到状态
    val signInState = mutableStateOf(getSignInState())

    /**
     * 获取签到状态
     */
    private fun getSignInState(): Boolean {
        return TimeUtils.isToday(MmkvUtils.get(signInStateKey, "").toLongOrDefault(0L))
    }

    /**
     * 保存签到状态
     */
    fun saveSignInState() {
        if (signInState.value) {
            ToastUtils.showShort("今日已签到")
        } else {
            signInState.value = true
            MmkvUtils.save(signInStateKey, "${System.currentTimeMillis()}")
            ToastUtils.showShort("签到成功")
        }
    }

    val danCiDao by lazy { DanCiDataBase.getInstance().getDanCiDao() }

    //当前词库
    val currentKind = mutableStateOf("")

    //打卡次数
    val numberOfPunches = mutableIntStateOf(0)
    var numberOfPunches1 = 0
    //今日单词
    val wordOfTheDay = mutableStateOf("0")

    var wordOfTheDay1 = "0"

    //剩余单词
    val remainingWords = mutableStateOf("0")
    var remainingWords1 = ""
    //剩余天数
    val numberOfDaysRemaining = mutableStateOf("0")
    var numberOfDaysRemaining1 = ""
    //已学单词数量
    val theNumberOfWordsLearned = mutableIntStateOf(0)
    //单词数量
    val numberOfWords = mutableIntStateOf(0)

    fun refreshTheData(){
        viewModelScope.launch(Dispatchers.IO) {
            currentKind.value = danCiDao.queryMyStackByFlag(true)?: "研究生"
            DanCiData.getJiHuaList(currentKind.value).also {
                theNumberOfWordsLearned.intValue = danCiDao.queryMyWordCountByKind(currentKind.value)
                numberOfWords.intValue = danCiDao.queryCountByKind(currentKind.value).toInt()
                if (it.isEmpty()) {
                    wordOfTheDay.value = "暂无计划"
                    remainingWords.value = "暂无计划"
                    numberOfDaysRemaining.value = "暂无计划"
                    wordOfTheDay1 = "0"
                    remainingWords1 = "0"
                    numberOfDaysRemaining1 = "0"
                    numberOfPunches.intValue = 0
                    numberOfPunches1 = 0
                } else {
                    it[0].apply {
                        //共计天数
                        val day = ((end_time - start_time) / 1000 / (60 * 60 * 24) + 1)
                        //今日单词（单次数量/天数）
                        wordOfTheDay.value = "${count / day}"
                        //剩余单词（单次数量-今日单词）
                        remainingWords.value = "${numberOfWords.intValue - theNumberOfWordsLearned.intValue}"
                        //剩余天数(结尾时间减去当天)
                        numberOfDaysRemaining.value = "${(end_time - System.currentTimeMillis()) / 1000 / (60 * 60 * 24) + 1}"

                        numberOfPunches.intValue = this.day
                        numberOfPunches1 = this.day
                        wordOfTheDay1 = "${count / day}"
                        remainingWords1 = "${numberOfWords.intValue - theNumberOfWordsLearned.intValue}"
                        numberOfDaysRemaining1 = "${(end_time - System.currentTimeMillis()) / 1000 / (60 * 60 * 24) + 1}"
                    }
                }
            }
        }
    }
}